//
//  THJCompanyVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/2.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJCompanyVC.h"
#import "THJCompanyProductView.h"
#import "THJCompanyInfoView.h"
#import "THJCompanyHonorView.h"
#import "THJProductDetailVC.h"
#import "ZBNetworking.h"
@interface THJCompanyVC ()<THJCompanyProductViewDelegate>
{
    THJCompanyProductView * productView;
    THJCompanyInfoView * infoView;
    THJCompanyHonorView * honorView;
    NSInteger btnClickIndex;
    UIButton * phoneBtn;
    NSInteger favBtnFlag;
}
@property(nonatomic,strong)NSDictionary * companyInfoDic;
@property (weak, nonatomic) IBOutlet UILabel *companyNameLab;
@property (weak, nonatomic) IBOutlet UIImageView *companyLogoImgV;
@property (weak, nonatomic) IBOutlet UIButton *companyFavBtn;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UIImageView *headImgV;


@end

@implementation THJCompanyVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    [self.tabBarController.tabBar setHidden:YES];
    //状态栏颜色
    [self setStateBarColor:[UIColor clearColor]];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [phoneBtn removeFromSuperview];
}
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    //设置阴影
    self.headBgView.layer.shadowColor = [UIColor grayColor].CGColor;//设置阴影的颜色
    self.headBgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
    self.headBgView.layer.shadowOffset = CGSizeMake(3, 3);//设置阴影的偏移量
    self.headBgView.layer.shadowRadius = 3;//设置阴影的圆角
    //btn部分圆角
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.btn1.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.btn1.bounds;
    maskLayer.path = maskPath.CGPath;
    self.btn1.layer.mask = maskLayer;
    
    
    btnClickIndex = 1;
    [self setGoBackBtn];
    [self getData];
}
-(void)getData{
    __weak typeof(self) weakself = self;
    NSString * urlstr = [NSString stringWithFormat:@"%@index.php?f=company&r=company&m=info&cid=%@",THJUrlDomain,self.cid];
    NSLog(@"公司详情url-%@",urlstr);
    
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
        [request setValue:[THJTools getToken] forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            weakself.companyInfoDic = receiveData[@"data"];
            [weakself setUI];
        }
        
    } failed:^(NSError *error){
        
    }];
}

-(void)setUI{
    NSLog(@"gognsi-%@",self.companyInfoDic);
    self.companyNameLab.text = self.companyInfoDic[@"company"];
    [self.companyLogoImgV sd_setImageWithURL:[NSURL URLWithString:self.companyInfoDic[@""]] placeholderImage:kGetImage(@"占位图")];
    self.addressLab.text = self.companyInfoDic[@"address"];
    if([self.companyInfoDic[@"fav_flag"] isEqual:@"0"]){
        favBtnFlag = 0;
        [self.companyFavBtn setImage:kGetImage(@"收藏")  forState:UIControlStateNormal];
    }else{
        favBtnFlag = 1;
        [self.companyFavBtn setImage:kGetImage(@"收藏2")  forState:UIControlStateNormal];
    }
    [self.headImgV sd_setImageWithURL:[NSURL URLWithString:self.companyInfoDic[@"banner"]] placeholderImage:kGetImage(@"headBgPic")];
    productView = [[THJCompanyProductView alloc] initWithId:self.cid];
    productView.frame=CGRectMake(0, 240, ScreenWidth, ScreenHeight-240);
    productView.delegate = self;
    [self.view addSubview:productView];
    
    [self setBtnBackgroundColorWithBtn:self.btn1];
    //设置btn 标题和图片位置
    for (NSInteger i = 0; i<3; i++) {
        UIButton * btn = self.headBgView.subviews[i];
        [self setButtonContentCenter:btn];
    }
    
    //打电话按钮
    phoneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    phoneBtn.frame = CGRectMake(ScreenWidth - 60, ScreenHeight - 60, 50, 50);
    [phoneBtn setBackgroundImage:kGetImage(@"dadianhua") forState:UIControlStateNormal];
    [phoneBtn addTarget:self action:@selector(dadianhua) forControlEvents:UIControlEventTouchUpInside];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:phoneBtn];
}
#pragma mark -- THJCompanyProductViewDelegate --
-(void)cellClickedItemid:(NSString *)itemid{
    THJProductDetailVC * vc = [[THJProductDetailVC alloc] init];
    vc.itemid = itemid;
    [self.navigationController pushViewController:vc animated:YES];
   
}
/**
 打电话
 */
-(void)dadianhua{
    NSLog(@"打电话");
    NSString * str=@"telprompt://400-999-6014";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (IBAction)menuClick:(id)sender {
    NSLog(@"菜单点击");
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 4;
}
/**
 旗下产品
 @param sender btn
 */
- (IBAction)btn1Click:(UIButton*)sender {
    if(btnClickIndex == 1){
        return;
    }
    btnClickIndex = 1;
    [self setBtnBackgroundColorWithBtn:sender];
    self.headBgView.backgroundColor = COLOR(224, 225, 226, 1);
    productView = [[THJCompanyProductView alloc] initWithId:self.cid];
    productView.delegate = self;
    productView.frame=CGRectMake(0, 240, ScreenWidth, ScreenHeight-240);
    [self.view addSubview:productView];
    [infoView removeFromSuperview];
    [honorView removeFromSuperview];
}
/**
 关于

 @param sender btn
 */
- (IBAction)btn2Click:(UIButton*)sender {
    if(btnClickIndex == 2){
        return;
    }
    btnClickIndex = 2;
    [self setBtnBackgroundColorWithBtn:sender];
    [productView removeFromSuperview];
    [honorView removeFromSuperview];
    infoView = [[THJCompanyInfoView alloc] initWithDic:self.companyInfoDic];
    infoView.frame = CGRectMake(0, 240, ScreenWidth, ScreenHeight-240);
    [self.view addSubview:infoView];
}
/**
 荣誉资质

 @param sender btn
 */
- (IBAction)btn3Click:(UIButton*)sender {
    if(btnClickIndex == 3){
        return;
    }
    btnClickIndex = 3;
    [self setBtnBackgroundColorWithBtn:sender];
    self.headBgView.backgroundColor = COLOR(224, 225, 226, 1);
    [productView removeFromSuperview];
    [infoView removeFromSuperview];
    honorView = [THJCompanyHonorView instancView];
    honorView.cid = self.cid;
    honorView.companyInfoDic = self.companyInfoDic;
    honorView.frame = CGRectMake(0, 240, ScreenWidth, ScreenHeight-240);
    [self.view addSubview:honorView];
}
-(void)setBtnBackgroundColorWithBtn:(UIButton*)btn{
    
    for (NSInteger i = 0; i<3; i++) {
        UIButton * btn = self.headBgView.subviews[i];
        [btn setBackgroundColor:[UIColor whiteColor]];
    }
    self.headBgView.backgroundColor = [UIColor whiteColor];
    [btn setBackgroundColor:COLOR(224, 225, 226, 1)];
}

/**
 收藏
 */
- (IBAction)addFav:(id)sender {
    NSString * token = [THJTools getToken];
    NSLog(@"是否收藏-%ld",favBtnFlag);
    if(![THJTools IsLogIn]){
        [self HUDShowMsg:@"请先登录" AfterDelayDimiss:1];
        return;
    }
    if(favBtnFlag){
        [self.companyFavBtn setImage:kGetImage(@"收藏") forState:UIControlStateNormal];
        //取消收藏
        NSString * str = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=fav&r=fav&m=del"];
        [THJNetWork postDataFromUrl:str parameters:@{@"cid":self.cid} Token:token callback:^(NSDictionary* receiveData, NSError *error) {
            NSLog(@"收藏data-%@-%@",receiveData,self.cid);
            [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        }];
        favBtnFlag = 0;
    }else{
        //添加搜藏
        [self.companyFavBtn setImage:kGetImage(@"收藏2") forState:UIControlStateNormal];
        if(token.length){
            NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=fav&r=fav&m=add"];
            [THJNetWork postDataFromUrl:url parameters:@{@"cid":self.cid} Token:token callback:^(NSDictionary* receiveData, NSError *error) {
                NSLog(@"rece-%@",receiveData);
                [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            }];
        }else{
            [self HUDShowMsg:@"请先登录" AfterDelayDimiss:1];
        }
        favBtnFlag = 1;
    }
    
}
@end
